//:C05:CountedClass.cpp
// Object counting via static members.
#include <iostream>
using namespace std;

class CountedClass
{
    static int count;
    public:
        CountedClass() {++count;}
        CountedClass(const CountedClass&) {++count;}
        ~CountedClass(){--count;}
        static int getCount(){return count;}
};

int CountedClass::count = 0;

int main(void)
{
    CountedClass a;
    cout << CountedClass::getCount() << endl;   // 1
    CountedClass b;
    cout << CountedClass::getCount() << endl;   // 2
    {
        // An arbitrary scope:
        CountedClass c(b);
        cout << CountedClass::getCount() << endl;   // 3
        a = c;
        cout << CountedClass::getCount() << endl;   // 3
    }
    cout << CountedClass::getCount() << endl;       // 2
}
